from django.shortcuts import render
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import  UpdateModelMixin, RetrieveModelMixin
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.tsukkomi.models import Spit



from apps.tsukkomi.serializer import GetlistSerializer, PutShouCangSerzer, PutDianZanSerializer, PinglunSerializer


# 查询吐槽,发布
class Getlist(ModelViewSet):
    queryset = Spit.objects.all()
    serializer_class = GetlistSerializer

# # 详情
class XiangQING(ModelViewSet):
    queryset = Spit.objects.all()
    serializer_class = GetlistSerializer

# 评论
class Pinglun(RetrieveModelMixin,GenericAPIView):
    queryset = Spit.objects.all()
    serializer_class = PinglunSerializer
    def get(self, request, pk):
        return self.retrieve(request)






# 收藏
class PutShouCang(UpdateModelMixin, GenericAPIView):
    queryset = Spit.objects.all()
    serializer_class = PutShouCangSerzer
    def put(self,request,pk):
        instance = self.get_object()
        try:
            instance.collected=False if instance.collected else True
            instance.save()
        except:
            return Response(data={"messages":"添加失败"},status=status.HTTP_400_BAD_REQUEST)
        return Response(data=self.get_serializer(instance).data,status=status.HTTP_200_OK)


# 点赞
class PutDianZan(UpdateModelMixin,GenericAPIView):
    queryset = Spit.objects.all()
    serializer_class = PutDianZanSerializer
    def put(self, request, pk):
        instance = self.get_object()
        try:
            instance.hasthumbup=False if instance.hasthumbup else True
            instance.save()
        except:
            return Response(data={'messages':'添加失败'},status=status.HTTP_400_BAD_REQUEST)
        return Response(data=self.get_serializer(instance).data,status=status.HTTP_200_OK)



